package com.yeren.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yeren.dto.OriginalUserInfoDTO;
import com.yeren.dto.SNSUserInfoDTO;
import com.yeren.dto.WeixinOauth2TokenDTO;
import com.yeren.entity.WeixinUserInfo;
import com.yeren.service.GetTokenService;
import com.yeren.service.GetUserInfoService;
import com.yeren.service.WeixinUserInfoService;
import com.yeren.service.impl.WeixinUserInfoServiceImpl;
import com.yeren.utils.CommonUtil;

/**
 * 授权后的回调请求处理
 * 
 * @author liubiao
 *
 */
public class OAuthServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 用户同意授权后，能获取到code
		String code = request.getParameter("code");
		System.out.println("code::::::::::::::::::::::::::::::::::::::::::"
				+ code);
		// 用户同意授权
		if (!"authdeny".equals(code)) {
			// 获取网页授权access_token
			WeixinOauth2TokenDTO weixinOauth2Token = GetTokenService
					.getOauth2AccessToken("wxf9b54a0426aab166",
							"8481d5df777deb142eb3a18b2691f95b", code);
			System.out
					.println("weixinOauth2Token::::::::::::::::::::::::::::::::::::::::::"
							+ weixinOauth2Token);
			// 网页授权接口访问凭证
			String accessToken1 = weixinOauth2Token.getAccessToken();
			System.out
					.println("accessToken::::::::::::::::::::::::::::::::::::::::::"
							+ accessToken1);
			// 用户标识
			String openId = weixinOauth2Token.getOpenId();
			System.out
					.println("openId::::::::::::::::::::::::::::::::::::::::::"
							+ openId);
			// 获取用户信息1
			SNSUserInfoDTO snsUserInfoDTO = GetUserInfoService.getSNSUserInfo(
					accessToken1, openId);
			System.out
					.println("snsUserInfo::::::::::::::::::::::::::::::::::::::::::"
							+ snsUserInfoDTO);

			String accessToken2 = GetTokenService.getToken("wxf9b54a0426aab166",
					"8481d5df777deb142eb3a18b2691f95b").getAccessToken();
			// 获取用户信息2
			OriginalUserInfoDTO originalUserInfo = GetUserInfoService.getUserInfo(
					accessToken2, openId);
			System.out
					.println("originalUserInfo::::::::::::::::::::::::::::::::::::::::::"
							+ originalUserInfo);
			// 存储用户信息ALL
			WeixinUserInfo weixinUserInfo = new WeixinUserInfo();
			weixinUserInfo.setOpenId(originalUserInfo.getOpenId());
			weixinUserInfo.setSubscribe(originalUserInfo.getSubscribe());
			weixinUserInfo
					.setSubscribeTime(originalUserInfo.getSubscribeTime());
			weixinUserInfo.setNickname(originalUserInfo.getNickname());
			weixinUserInfo.setSex(originalUserInfo.getSex());
			weixinUserInfo.setCountry(originalUserInfo.getCountry());
			weixinUserInfo.setProvince(originalUserInfo.getProvince());
			weixinUserInfo.setCity(originalUserInfo.getCity());
			weixinUserInfo.setLanguage(originalUserInfo.getLanguage());
			weixinUserInfo.setHeadImgUrl(originalUserInfo.getHeadImgUrl());
			// 最后一项注意
			weixinUserInfo.setPrivileges(snsUserInfoDTO.getPrivilegeList()
					.toString());
			WeixinUserInfoService weixinUserInfoService = new WeixinUserInfoServiceImpl();

			boolean isExist = weixinUserInfoService.isExitOpenId(weixinUserInfo
					.getOpenId());
			System.out.println("isExist=====>>" + isExist);
			if (isExist) {
				// 存在就更新
				weixinUserInfoService.update(weixinUserInfo);
			} else {
				// 不存在就保存
				weixinUserInfoService.save(weixinUserInfo);
			}
			// 设置要传递的参数
			request.setAttribute("snsUserInfoDTO", snsUserInfoDTO);

		}
		// 跳转到index.jsp
		request.getRequestDispatcher("weixinSimpleUserInfoList.jsp").forward(
				request, response);

	}
}
